package com.cat.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cat.entity.Community;
import com.cat.entity.Dict;
import com.cat.service.CommunityService;
import com.cat.service.DictService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/community")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CommunityController extends BaseController{
    @Reference
    private CommunityService communityService;

    @Reference
    private DictService dictService;
    private final static String LIST_ACTION = "redirect:/community";

    private final static String PAGE_INDEX = "community/index";
    private final static String PAGE_SHOW = "community/show";
    private final static String PAGE_CREATE = "community/create";
    private final static String PAGE_EDIT = "community/edit";
    private final static String PAGE_SUCCESS = "common/successPage";
    /**
     * 列表
     * @param map
     * @param request
     * @return
     */
    @RequestMapping
    public String index(Map map, HttpServletRequest request) {
        Map<String,Object> filters = getFilters(request);
        PageInfo<Community> page = communityService.findPage(filters);

        List<Dict> areaList = dictService.findDictByDictCode("beijing");
        map.put("areaList",areaList);
        map.put("page", page);
        map.put("filters", filters);
        return PAGE_INDEX;
    }
    @RequestMapping("/create")
    public String create(Map map){
        List<Dict> areaList = dictService.findDictByDictCode("beijing");
        map.put("areaList",areaList);
        return PAGE_CREATE;
    }
    @PostMapping("/save")
    public String save(Community community) {
        communityService.insert(community);
        return PAGE_SUCCESS;
    }
    @GetMapping("/edit/{id}")
    public String edit(Map map, @PathVariable Long id){
        Community community = communityService.getById(id);
        List<Dict> areaList = dictService.findDictByDictCode("beijing");
        map.put("community",community);
        map.put("areaList",areaList);
        return PAGE_EDIT;
    }
    @PostMapping(value="/update")
    public String update(Community community) {

        communityService.update(community);

        return PAGE_SUCCESS;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        communityService.delete(id);
        return LIST_ACTION;
    }
}
